from lxml import etree
from util.selenium.webdriver.common.by import By
from util.selenium.webdriver.support.wait import WebDriverWait
from util.selenium.webdriver.support import expected_conditions as EC

# from util.db_operate import SaveData
from spider_data.spiderBaseMethod import bm
from util.g import g


# from util.handle_list import bm.get_first


def parse_hr(driver=None):
    try:
        bm.detail_switch_iframe(driver=driver)
        main_case_loc = (By.LINK_TEXT, '人力资源情况表')

        # # # 点击 主要情况表按钮。

        bm.wait(driver=driver, ele_loc=main_case_loc)

        driver.find_element(*main_case_loc).click()
        # # # 等待标题加载出来
        bm.wait(driver=driver, ele_loc=g.title_loc)

        text = driver.page_source

        tree = etree.HTML(text)

        return parse_and_save(tree=tree)
        # driver.switch_to_default_conttree()
    except Exception as err:
        msg = "error: {} file: {} lines: {}".format(err, err.__traceback__.tb_frame.f_globals["__file__"],
                                                    err.__traceback__.tb_lineno)
        g.logger.error(msg)
        raise Exception(msg)


def parse_and_save(tree=None):
    try:
        # hrinfo = bm.get_first(tree.xpath('//div[contains(@class, "box-body")]/div[1]'))

        # # 解析企业职工人数
        # tree = bm.get_first(hrinfo.xpath('./table[1]/tbody/tr'))
        #      人员总数    在职人员    兼职人员    临时聘用人员      外籍人员    留学归国人员      千人计划人员
        tree_item = {'type': 1, 'total': '', 'onjob': '', 'parttime': '',
                     'tempstaff': '', 'foreigner': '', 'returnees': '', 'thstaltrees': ''}
        tree_item['total'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrTotal']/@value"))
        tree_item['onjob'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrOnJob']/@value"))
        tree_item['parttime'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrPart']/@value"))
        tree_item['foreigner'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrExt']/@value"))
        tree_item['tempstaff'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrForg']/@value"))
        tree_item['returnees'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrAbroed']/@value"))
        tree_item['thstaltrees'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrTsdplan']/@value"))

        # # 解析企业科技人员数
        # tree = bm.get_first(hrinfo.xpath('./table[2]/tbody/tr'))
        tree_item = {'type': 2, 'total': '', 'onjob': '', 'parttime': '',
                     'tempstaff': '', 'foreigner': '', 'returnees': '', 'thstaltrees': ''}
        tree_item['total'] = bm.get_int(int_str=tree.xpath("//input[@id='satHrTotal']/@value"))
        tree_item['onjob'] = bm.get_int(int_str=tree.xpath("//input[@id='satHrOnJob']/@value"))
        tree_item['parttime'] = bm.get_int(int_str=tree.xpath("//input[@id='satHrPart']/@value"))
        tree_item['foreigner'] = bm.get_int(int_str=tree.xpath("//input[@id='satHrExt']/@value"))
        tree_item['tempstaff'] = bm.get_int(int_str=tree.xpath("//input[@id='satHrForg']/@value"))
        tree_item['returnees'] = bm.get_int(int_str=tree.xpath("//input[@id='satHrAbroed']/@value"))
        tree_item['thstaltrees'] = bm.get_int(int_str=tree.xpath("//input[@id='satHrTsdplan']/@value"))
        # print(tree_item)
        # SaveData().save(table='tree_res_hrinfo', value=tree_item)
        # # print(tree_item)
        # SaveData().save(table='tree_res_hrinfo', value=tree_item)

        # # # 解析 企业全体人员结构 数据

        # hrsubinfo = bm.get_first(tree.xpath('//div[contains(@class, "box-body")]/div[2]'))
        hrsubinfo_item = {'doctor': '', 'md': '', 'bd': '', 'coll': '', 'senior': '', 'medium': '', 'primary': '',
                          'snengr': '', 'age30al': '', 'age40al': '', 'age50al': '', 'age51au': ''}

        # edu 学历
        # edu = bm.get_first(hrsubinfo.xpath('./table[1]/tbody/tr'))
        hrsubinfo_item['doctor'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrDoctor']/@value"))
        hrsubinfo_item['md'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrMaster']/@value"))
        hrsubinfo_item['bd'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrBachelor']/@value"))
        hrsubinfo_item['coll'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrJunior']/@value"))

        # tit 职称
        # tit = bm.get_first(hrsubinfo.xpath('./table[2]/tbody/tr'))
        hrsubinfo_item['senior'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrSenior']/@value"))
        hrsubinfo_item['medium'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrMiddle']/@value"))
        hrsubinfo_item['primary'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrPrimary']/@value"))
        hrsubinfo_item['snengr'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrMechanic']/@value"))

        # age 年龄
        # age = bm.get_first(hrsubinfo.xpath('./table[3]/tbody/tr'))
        hrsubinfo_item['age30al'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrAge1']/@value"))
        hrsubinfo_item['age40al'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrAge2']/@value"))
        hrsubinfo_item['age50al'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrAge3']/@value"))
        hrsubinfo_item['age51au'] = bm.get_int(int_str=tree.xpath("//input[@id='entHrAge4']/@value"))

        # print(hrsubinfo_item)
        # SaveData().save(table='tree_res_hrsubinfo', value=hrsubinfo_item)
        return {"key": "WriteHrForm",
                "value": {"WriteHrForm": [tree_item, tree_item], "WriteHRSubinfo": hrsubinfo_item}}
    except Exception as err:
        msg = "error: {} file: {} lines: {}".format(err, err.__traceback__.tb_frame.f_globals["__file__"],
                                                    err.__traceback__.tb_lineno)
        g.logger.error(msg)
        raise Exception(msg)
